//
//  EHLoginViewController.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/12.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import SwiftFontName

public class EHLoginViewController: UIViewController {
    @IBOutlet weak var nameTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    @IBOutlet weak var loginButton: UIButton! {
        didSet {
            loginButton.layer.cornerRadius = 8
            loginButton.layer.masksToBounds = true
        }
    }

    let viewModel = LoginViewModel()
    let disposeBag = DisposeBag()

    public override func viewDidLoad() {
        super.viewDidLoad()
        setupNavibar()
        bindData()
    }

    @IBAction func forgetPassword(_ sender: UIButton) {
    }

    private func bindData() {
        nameTF.rx.text
            .orEmpty
            .bind(to: viewModel.username)
            .disposed(by: disposeBag)

        passwordTF.rx.text
            .orEmpty
            .bind(to: viewModel.password)
            .disposed(by: disposeBag)

        viewModel.isValid.map { $0 }
            .bind(to: loginButton.rx.isEnabled)
            .disposed(by: disposeBag)
//        loginButton.rx.tap
//            .subscribe { [unowned self] _ in
//                print("tap")
//                self.viewModel.logIn(bag: self.disposeBag)
//            }.disposed(by: disposeBag)
    }

    private func setupNavibar() {
        navigationController?.navigationBar.applyGradient()
        let backButtom = UIBarButtonItem(image: nil, style: .plain, target: self, action: nil)
        navigationItem.leftBarButtonItem = backButtom
        let titleLabel = UILabel()
        titleLabel.bounds = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width - 120, height: 40))
        titleLabel.text = "登录"
        titleLabel.textColor = .white
        titleLabel.font = UIFont(name: FontName.PingFangSCMedium, size: 18)
        navigationItem.titleView = titleLabel
    }
}
